<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %><%@ page pageEncoding="UTF-8"%>
RegionUtil = {
    data: ${regionsJson},
    exists: function(id){
        if(this.data[id]==undefined){
            return false;
        }
        return true
    },
    getItem: function(id){
    	return this.data[id];
    },
    getSub: function(id){
        if(!this.exists(id)){
                alert('can not find item with id ' + id);
                return null;
        }
        var obj = {};
        var exp = new RegExp('^' + id + '\\d{3}$');
        for(var i in this.data){
            if(exp.test(i)){
                obj[i] = this.data[i];
            }
        }
        for(var i in obj){
            return obj;
        }
        return null;
    },
    getParent: function(id){
        if(!this.exists(id)){
            alert('error in getParent, can not find region with id: ' + id);
            return null;
        }
        if(id.length<=3){
            alert('No parent for the top level region.');    
            return null;
        }
        var pid = id.substring(0, id.length-3);
        
        if(!this.exists(pid)){
                alert('error in getParent, can not find region with id: ' + pid);
                return null;
        }
        var obj = {};
        obj[pid] = this.data[pid];
        return obj;
    },
    doRegionSelChange: function(e){
        var v = $(e).val();
        if(v!=''){
            $('#regionId').val(v);
            while($(e).next().length>0){
                $(e).next().remove();
            }
            $(RegionUtil.createRegionSelHtml(RegionUtil.getSub(v))).appendTo($('#regionSelectorContainer'));
        }
        else{
            $('#regionId').val('');
            while($(e).next().length>0){
                $(e).next().remove();
            }
        }
    },
    createRegionSelHtml: function(regions, selValue){
        if(regions==null){
            return '';
        }
        var html = '<select onchange="doRegionSelChange(this);"><option value="">请选择...</option>';
        for(var r in regions){
            var sel = "";
            if(selValue==r){
                sel = "selected";
            }
            html += '<option ' + sel + ' value="' + r + '">' + regions[r] + '</option>';
        }
        html += '</select>';
        return html;
    },
    getPath: function(id){
    	var selectedRegionId;
    	if(id!=null&&id!=undefined){
    		selectedRegionId=id;
    	}
    	else{
    		selectedRegionId = $('#regionId').val();
    	}
    	var p = '';
    	var totalLength = 3;
    	var pid;
    	while(totalLength<=selectedRegionId.length){
    		pid = selectedRegionId.substring(0, totalLength);
    		p += this.getItem(pid)+'/';
    		totalLength += 3;
    	}
    	return p;
    },
    restoreSel: function(){
        var selectedRegionId = $('#regionId').val();
        if(selectedRegionId==''){
            $(RegionUtil.createRegionSelHtml(RegionUtil.getSub('100'))).appendTo($('#regionSelectorContainer'));
        }
        else{
            var totalLength = 3;
            var pid;
            var selVal;
            while(totalLength<=selectedRegionId.length){
                pid = selectedRegionId.substring(0, totalLength);
                selVal = selectedRegionId.substring(0, totalLength + 3);
                $(RegionUtil.createRegionSelHtml(RegionUtil.getSub(pid), selVal)).appendTo($('#regionSelectorContainer'));
                totalLength += 3;
            }
        }
    }
};
window.doRegionSelChange = RegionUtil.doRegionSelChange;
